package com.atguigu.jdbc2;

import com.atguigu.jdbc.Employee;
import com.atguigu.jdbc.JDBCUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.junit.Test;

import java.sql.SQLException;
import java.util.List;

/**
 * @author 798133716@qq.com
 * @create 2021-02-26 20:25
 */
public class DBUtils {
    @Test
    public void test() throws SQLException {
        QueryRunner qr = new QueryRunner();
        String sql = "insert into demo1(id,username) values(?,?)";
        int sd = qr.update(JDBCUtils.getConnection(), sql, 100, "sd");
        System.out.println(sd);
    }
    @Test
    public void test1() throws SQLException {
        QueryRunner qr = new QueryRunner();
        //获取单条语句
//        String sql = "select id,username from demo8 where id = ?";
//        Employee emp = qr.query(JDBCUtils.getConnection(), sql, new BeanHandler<Employee>(Employee.class),2);
//        System.out.println(emp);
        //获取多条语句
        String sql = "select id sid,username susername from demo8";
        List<Employee> query = qr.query(JDBCUtils.getConnection(), sql, new BeanListHandler<Employee>(Employee.class
        ));
        for (Employee employee : query) {
            System.out.println(employee);
        }

    }
}
